package com.briup.day18;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Test_Catch {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr={4,5,6,7,8};
        try{
            int index = sc.nextInt();
            System.out.println(arr[index]);
        }catch (InputMismatchException|ArrayIndexOutOfBoundsException|NullPointerException e){//不能有父类
                if(e instanceof ArrayIndexOutOfBoundsException){
                    System.out.println("数组索引越界");
                }else if(e instanceof InputMismatchException){
                    System.out.println("输入格式有误");
                }else if(e instanceof NullPointerException){
                    System.out.println("空指针异常");
                }
                e.printStackTrace();
        }
    }
    public static void main01(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = {1, 2, 3, 4, 5};
        try {
            //可能出现异常
            int index = sc.nextInt();
            System.out.println(arr[index]);
        } catch (ArrayIndexOutOfBoundsException e) {
            //捕获异常并处理
            System.out.println("hello");
            e.printStackTrace();//将异常信息输出到控制台
            System.out.println("继续运行");
            System.out.println(e.getMessage());
            System.out.println(e.toString());
        } catch (InputMismatchException e) {
            System.out.println("in catch, 捕获键盘录入格式有误异常成 功...");
            e.printStackTrace();
        }
        System.out.println("after try-catch ...");
    }
}
